/**
 * 检测某个元素是否存在于某个数组中
 * @param value
 * @param array
 * @returns {boolean}
 */
function in_array(value,array){
	for(var i=0;i<array.length;i++) {
		if(value == array[i])return true;
	}
	return false;
}

/** 消息提示 */
var Message = {
	add : function (option){
		if(option == undefined)
			var option = {};
		else
			var option = option;
		var defaultv = {
			"message":"提示消息",
			"style":{
				"position":"absolute",
				"top":($(window).height() - 40) / 2 + $(window).scrollTop() + "px",
				"left":($(window).width() - 350) / 2 + $(window).scrollLeft() + "px",
				"padding":"10px 9px 8px 14px",
				"width":"400px",
				"text-align":"center",
				"background":"#cd7fc6",
				"border-radius":"0px",
				"box-shadow":"0px 0px 100px #999",
				"z-index":9999,
				"color":"#fff",
				"font-family":"微软雅黑",
				"font-size":"16px"
			},
			"autoClose":true,
			"period":1500,
			"afterHide" : function (){}
		};
		if(option['style'] != undefined){
			option['style'] = $.extend(defaultv['style'],option['style']);
		};
		var op = $.extend(defaultv,option);
		var css = "";
		for(i in op['style']){
			css += i+":"+op['style'][i]+";";
		}
		if($("#alertMessage").length > 0) $("#alertMessage").remove();
		$("body").append("<div id='alertMessage' style='"+css+"'>"+op['message']+"</div>");

		if(op['autoClose']){
			setTimeout(function (){
				Message.remove();
			},op.period);
		}
		$("body").bind("afterHide",op.afterHide);
	},
	remove : function (){
		$("#alertMessage").fadeOut(700,function (){
			$(this).remove();
			$("body").trigger("afterHide");
		});
	}
};
/**post ajax*/
function post(action,send,callback){
	var opt = {
		url : action,
		data : send,
		type : 'post',
		dataType : 'json',
		success : function(json){
			callback(json);
		},
		beforeSend : function (){
			if(arguments[3])arguments[3]();
			//if(!arguments[3])Message.add({message:'正在处理,请稍等...'});
		},
		complete : function (){
			if(arguments[4])arguments[4]();
		},
		error : function(){
			if(arguments[5])arguments[5]();
		}
	};
	$.ajax(opt);
}



//表单验证
var VF = {
	UnderInputTip : function(msg,o){
		var ibox = o.obj.parent();
		ajaxPost : true;
		if(o.type == 2){ // sucess
			ibox.removeClass("form-error");
			ibox.parent().find(".tip").text("");
			ibox.find(".false").hide();
			ibox.find(".true").show();
		}
		if(o.type == 3){ // false
			ibox.addClass("form-error");
			ibox.parent().find(".tip").text(msg);
			ibox.find(".true").hide();
			ibox.find(".false").show();
		}
	}
}


//表单input状态高亮
$.fn.outline = function(){
		var ibox = $(this);
		ibox.find("input").focus(function(){
			$(this).parent().addClass("outline");
		}).blur(function(){
			$(this).parent().removeClass("outline");
		});
	}


/**提示模态框*/
/**表单模态框*/
/**
*只有关闭按钮
 * AlterTip.alert('确定删除该优惠卷么?')
 *有关闭按钮、确定按钮
 * AlterTip.confirm({
 *      msg : '确定删除该优惠卷',
 *      buttonText : "删除",
 *      click : function(){
 *         // 回调
 *      }
 * });
 * @type type
 */
var AlterTip = {
	//成功弹框（单选项）
    createHtml : function(msg){
        var html = '<div class="modal fade tipss" id="myModal02" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">'+
                   '<div class="modal-dialog" role="document">'+
                   '<div class="r-modal">';
            html += '<p style="margin: 0 0 30px 0" class="p"><img src="/project/style/images/label.png">'+msg+'</p>';
            html += '<p style="margin: 0 0 0px 36%" class="p" ><button type="button" class="btn r-madal-close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">关闭</span></button></p>'+   
                    '</div></div></div></div>';
            $("body").append(html);
    },
    alert: function(msg){
        $("#myModal02").remove();
        if($("#myModal02").length <=0){
            this.createHtml(msg);
        }else{
            $("#myModal02").find("p:eq(0)").text(msg).wrapAll('<p style="margin: 0 0 30px 0" class="p">'+msg+'</p>');
        }
        $("#myModal02").modal("show"); 
    },
	
	//警告弹框（单选项）
	createHtml2 : function(msg){
        var html = '<div class="modal fade tipss" id="myModal02" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">'+
                   '<div class="modal-dialog" role="document">'+
                   '<div class="r-modal">';
            html += '<p style="margin: 0 0 30px 0" class="p"><img src="/project/style/images/tanhao.png">'+msg+'</p>';
            html += '<p style="margin: 0 0 0px 36%" class="p" ><button type="button" class="btn r-madal-close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">关闭</span></button></p>'+   
                    '</div></div></div></div>';
            $("body").append(html);
    },
    alert2: function(msg){
        $("#myModal02").remove();
        if($("#myModal02").length <=0){
            this.createHtml2(msg);
        }else{
            $("#myModal02").find("p:eq(0)").text(msg).wrapAll('<p style="margin: 0 0 30px 0" class="p">'+msg+'</p>');
        }
        $("#myModal02").modal("show"); 
    },
	
	
	//警告弹框（双选项）
    confirm : function(options){
        var msg = options.msg;
        var type = options.buttonText;
        $("#myModal02").remove();
        var box = $("#myModal02");
        if(box.length == 0){
            var html = '<div class="modal fade tipss" id="myModal02" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">'+
                    '<div class="modal-dialog" role="document">'+
                    '<div class="r-modal">';
             html += '<p  style="margin: 0 0 30px 0" class="p"><img src="/project/style/images/tanhao.png"><span class="msg">'+msg+'</span></p>';
             html += '<p style="margin: 0 0 0px 19%" class="p"><button type="button" class="btn sureBtn r-madal-close"  aria-label="Close"  style="margin: 0 12px 0px 0"><span>'+type+'</span></button><button type="button" class="btn r-madal-close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">关闭</span></button></p>'+   
                     '</div></div></div>';

            $("body").append(html);
            box =  $("#myModal02");
        }

        box.find(".msg").html(msg);
        box.find(".sureBtn span").html(type);  

        box.modal("show");
        box.find(".sureBtn").unbind();
        box.find(".sureBtn").click(function(){
            box.modal("hide");
            setTimeout(function(){
                options.click();
            },600);
        });
    },
	
	//成功弹框（双选项）
    confirm2 : function(options){
        var msg = options.msg;
        var type = options.buttonText;
        $("#myModal02").remove();
        var box = $("#myModal02");
        if(box.length == 0){
            var html = '<style> .ma</style>'+'<div class="modal fade tipss" id="myModal02" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">'+
                    '<div class="modal-dialog" role="document">'+
                    '<div class="r-modal">';
             html += '<p  style="margin: 0 0 30px 0" class="p"><img src="/project/style/images/label.png"><span class="msg">'+msg+'</span></p>';
             html += '<p style="margin: 0 0 0px 19%" class="p"><button type="button" class="btn sureBtn r-madal-close"  aria-label="Close"  style="margin: 0 12px 0px 0"><span>'+type+'</span></button><button type="button" class="btn r-madal-close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">关闭</span></button></p>'+   
                     '</div></div></div>';

            $("body").append(html);
            box =  $("#myModal02");
        }

        box.find(".msg").html(msg);
        box.find(".sureBtn span").html(type);  

        box.modal("show");
        box.find(".sureBtn").unbind();
        box.find(".sureBtn").click(function(){
            box.modal("hide");
            setTimeout(function(){
                options.click();
            },600);
        });
    },
    
}

/**表单模态框*/
var Modal = {
	tmp : null,
    createHtml: function(msg){
      var html = '<div class="modal fade" id="myModal03" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">'+
                 '<div class="modal-dialog" role="document" style="margin:50px auto;">'+
                     '<div class="r-modal" style="padding:0;">' + 
                       '<div class="modal-header">'+ 
                           '<span class="close fs24 tip_color_09 fr cursor-pointer " data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></span>' +
                           '<span class="modal-title" id="myModalLabel">'+msg+'</span>'+
                       '</div>'+'<div class="r-content">'+'<div>'
                      '</div>'+
                 '</div></div>';
      $("body").append(html);
    },
   alert: function(id,msg){        
		if($("#myModal03").length <=0){
            this.createHtml(msg);
        }
		var dialog = $("#myModal03");
		if(this.tmp){
			var prevBox = $(this.tmp);
			prevBox.hide();
			$("body").append(prevBox);			
		}
        var box = $(id);
		box.show();
        dialog.find(".r-content").html(box);
		dialog.find(".modal-dialog").width(box.width());
        dialog.modal("show");
		this.tmp = id;
   }
}

/** 自动居中裁剪大小调整 
 * @param minWidth :最小宽度
 * @param minHeight : 最小高度
 * @param objImg : 要处理的图片元素对象
 * @return null
 * @create 2016-01-16 11:21:00
 * @author Zhihao.Li
 *
 *  @Ex:
 * 	<div style="width:150px;height:150px;overflow:hidden;">
		<img onload="AutoResizeImage(150,150,this)" src="/project/style/images/banner1.jpg" alt="" />
	</div>
 */
function AutoResizeImage(minWidth,minHeight,objImg){
	var img = new Image();
	img.src = objImg.src;
	var Ratio;
	var w = img.width;
	var h = img.height;
	
	if(( minWidth / w ) >= (minHeight / h)){
		Ratio = minWidth / w;
	}else{
		Ratio = minHeight / h;
	}
	
	objImg.height = h*Ratio;
	objImg.width = w*Ratio;
	objImg.style.marginLeft = -(w*Ratio - minWidth)/2;
	objImg.style.marginTop = -(h*Ratio - minHeight)/2;
}

/**
 * 清空表单
 * @param id 表单id
 */
function clearForm(id){
	var formObj = document.getElementById(id);
	if(formObj == undefined){
		return;
	}
	for(var i=0; i<formObj.elements.length; i++){
		switch(formObj.elements[i].type){
			case 'text':
			case 'password':
			case 'hidden':
			case 'date':
			case 'textarea':
			case 'email':
				formObj.elements[i].value = "";
				break;
			case 'radio':
				formObj.elements[i].checked = false;
				break;
			case 'checkbox':
				formObj.elements[i].checked = false;
				break;
			case 'select-one':
				formObj.elements[i].options[0].selected = true;
				break;
			case 'select-multiple':
				for(var j = 0; j < formObj.elements[i].options.length; j++){
					formObj.elements[i].options[j].selected = false;
				}
				break;
			case 'file':
				//formObj.elements[i].select();
				//document.selection.clear();
				// for IE, Opera, Safari, Chrome
				var file = formObj.elements[i];
				if(file.outerHTML){
					file.outerHTML = file.outerHTML;
				}else{
					file.value = ""; // FF(包括3.5)
				}
				break;
		}
	}
}

;$.fn.clearForm = function(includeHidden) {
	return this.each(function() {
		$('input,select,textarea', this).clearFields(includeHidden);    //this表示设置上下文环境，有多个表单时只作用指定的表单
	});
};
/*分页js*/
function ajaxPage(page,pEle,cb){
	this.condition = {'page':1,'num':-1};
	this.pageLen = 9;
	this.pageEle = pEle;
	this.setPage = function(p){
		var obj = this;
		if(!this.pageEle)return;
		if(!p || typeof(p.pages) == 'undefined')return;
		var pageLen = this.pageLen;
		p.pages = Math.floor(p.pages);
		p.current = Math.floor(p.current);
		if(p.pages <=1){obj.pageEle.html('');return;}
		var cb_clickpage = function(){};
		if(arguments[1] &&  typeof(arguments[1])=='function'){
			cb_clickpage = arguments[1];
		}
		if(p.num){
			obj.condition.num = p.num;
		}
		if(obj.condition.num <0){
			delete(obj.condition.num);
		}

		var pl = pageLen,
			pre = '',
			next = '';
			st = 1,
			en = 1,
			half = Math.floor(pl/2);
		if(p.pages > pl){
			st = p.current-half;
			en = p.current+half;
			if(st < 1){st=1;en=pl;}
			if(en > p.pages){
				en = p.pages;
				st = p.pages-pl+1;
			}
		}else{
			en = p.pages;
		}
		var html ='', i;
		for(i=st;i<=en;i++){
			if(i==p.current){
				html += "<a class='active' value='"+i+"'>"+i+"</a>";
			}else{
				html += "<a value='"+i+"'>"+i+"</a>";
			}
		}
		
		if(p.current < p.pages){
			next = "<a value='"+(p.current+1)+"'>›</a>";
		}else{
			next = "<a value='0'>›</a>";
		}
		if(p.current >1){
			pre = "<a value='"+(p.current-1)+"'>‹</a>";
		}else{
			pre = "<a value='0'>‹</a>";
		}
		if(p.current != 1){
			preone = "<a value='1'>‹‹</a>";
		}else{
			preone = "<a value='0'>‹‹</a>";
		}
		if(p.current != en){
			nextlast = "<a value='"+en+"'>››</a>";
		}else{
			c = "<a value='0'>››</a>";
		}
var go = "<input type='text' id='inputpage'  />"+"<span style='border:1px solid #ccc;display:inline-block;height:30px;line-height:30px;padding:0 12px;' id='gopage'>GO</span>";
obj.pageEle.html("<span class='total'>共有<span class='total_num'>"+p.pages+"</span>页，每页显示10条</span>"+preone+pre+'<div class="pageNum">'+html+'</div>'+next+nextlast+go);
$('#gopage').click(function(){
			var page = document.getElementById('inputpage').value;
			if(page){
			obj.condition.page = page;
			cb_clickpage(obj.condition);}
		});
obj.pageEle.find("a").not(".active").click(function(){
			var page = $(this).attr("value");
			if(page ==0 || page==undefined)return;
			obj.condition.page = page;
			cb_clickpage(obj.condition);
		});

	}
	this.setPage(page,cb);
}

/**
 * 获取地址参数，以json的格式返回
 */
function geturlar()
{
	var currenturl = location.href;
	var obj = {};
	var ta = [];
	if(currenturl.indexOf("?") > -1)
	{
		var ar = currenturl.split("?");
		var hash = ar[1].split("&");
		for(i in hash)
		{
			ta = hash[i].split("=");
			obj[ta[0]] = ta[1];
		}
	}
	return obj;
}

/**
 * 获取地址参数，还原字符串，以json的格式返回
 */
function get_decode_urlar()
{
	var currenturl = location.href;
	var obj = {};
	var ta = [];
	if(currenturl.indexOf("?") > -1)
	{
		var ar = currenturl.split("?");
		var hash = ar[1].split("&");
		for(i in hash)
		{
			ta = hash[i].split("=");
			obj[ta[0]] = decodeURIComponent(ta[1]);
		}
		
	}
	return obj;
}

/**
 * 序列化json对象
 */
function getcondition(obj)
{
	var condition = "";
	var tmpta = [];
	for(j in obj)
	{
		tmpta.push(j+"="+obj[j]); 
	}
	condition = tmpta.join("&");
	return condition;
}

(function (){
	var obj = get_decode_urlar();
	for(var i in obj){
		if(obj[i]){
			$(".box-"+i).find("a").removeClass("selected");
			$(".box-"+i).find("a:contains('"+obj[i]+"')").addClass("selected");
		}
		if(i == 'city'){
			vm.city = obj[i];
		}
	}
}).call(this);

//JQ转时间戳
(function($) {
    $.extend({
        myTime: {
            /**
             * 当前时间戳
             * @return <int>        unix时间戳(秒)  
             */
            CurTime: function(){
                return Date.parse(new Date())/1000;
            },
            /**              
             * 日期 转换为 Unix时间戳
             * @param <string> 2014-01-01 20:20:20  日期格式              
             * @return <int>        unix时间戳(秒)              
             */
            DateToUnix: function(string) {
                var f = string.split(' ', 2);
                var d = (f[0] ? f[0] : '').split('-', 3);
                var t = (f[1] ? f[1] : '').split(':', 3);
                return (new Date(
                        parseInt(d[0], 10) || null,
                        (parseInt(d[1], 10) || 1) - 1,
                        parseInt(d[2], 10) || null,
                        parseInt(t[0], 10) || null,
                        parseInt(t[1], 10) || null,
                        parseInt(t[2], 10) || null
                        )).getTime() / 1000;
            },
            /**              
             * 时间戳转换日期              
             * @param <int> unixTime    待时间戳(秒)              
             * @param <bool> isFull    返回完整时间(Y-m-d 或者 Y-m-d H:i:s)              
             * @param <int>  timeZone   时区              
             */
            UnixToDate: function(unixTime, isFull, timeZone) {
                if (typeof (timeZone) == 'number')
                {
                    unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60;
                }
                var time = new Date(unixTime * 1000);
                var ymdhis = "";
                ymdhis += time.getUTCFullYear() + "-";
                ymdhis += (time.getUTCMonth()+1) + "-";
                ymdhis += time.getUTCDate();
                if (isFull === true)
                {
                    ymdhis += " " + time.getUTCHours() + ":";
                    ymdhis += time.getUTCMinutes() + ":";
                    ymdhis += time.getUTCSeconds();
                }
                return ymdhis;
            }
        }
    });
})(jQuery);
//获取地址栏参数
function accept_get(){
		alert(1);
	}

